package com.hopes.bizz.emergency.support;

/**
 * @author cx
 * @date 2024/1/17 16:46
 * @description:
 */

import com.hopes.bizz.common.core.util.DateUtils;
import com.hopes.bizz.emergency.common.constant.PlanDicConstant;
import lombok.RequiredArgsConstructor;

import java.util.Date;
import java.util.function.BiConsumer;
import java.util.function.Function;

/**
 * @author cx
 * @date 2024/1/17 16:46
 * @description:
 */
@RequiredArgsConstructor
public class AboutExpireSupport {

    /**
     * 过期状态设置
     * @param erEmergency
     * @param dueTimeFunction
     * @param statusSetter
     * @param <T>
     */
    public static <T,U> void setExpireStatus(T erEmergency, Function<T, U> dueTimeFunction, BiConsumer<T, Integer> statusSetter) {

        try {
            // 使用反射获取 dueTime
            U dueTime = dueTimeFunction.apply(erEmergency);
            //根据需要进行类型转换
            Date dueTimeDate = (Date) dueTime;
            long betweenDays = DateUtils.getIntervalDays(new Date(), dueTimeDate);
            statusSetter.accept(erEmergency, calculateStatus(betweenDays));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 计算状态
     * @param betweenDays
     * @return
     */
    private static int calculateStatus(long betweenDays) {
        if (betweenDays < 0) {
            // 过期
            return PlanDicConstant.EXPIRED;
        } else if (betweenDays <= PlanDicConstant.EXPIRED_LIMIT) {
            // 60天过期
            return PlanDicConstant.APPROACH_EXPIRED;
        } else {
            // 正常
            return PlanDicConstant.NORMAL;
        }
    }


}

